package cn.kshost.zqyg.manager.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.kshost.zqyg.manager.exception.ZhongQiaoException;
import cn.kshost.zqyg.manager.mapper.BrandCategoryMapper;
import cn.kshost.zqyg.manager.service.BrandCategoryService;
import cn.kshost.zqyg.model.dto.product.CategoryBrandDto;
import cn.kshost.zqyg.model.entity.product.Brand;
import cn.kshost.zqyg.model.entity.product.CategoryBrand;
import cn.kshost.zqyg.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BrandCategoryServiceImpl implements BrandCategoryService {

    @Autowired
    BrandCategoryMapper brandCategoryMapper;
    @Override
    public PageInfo<CategoryBrand> getBrandCategoryByPage(int pageNum, int pageSize, CategoryBrandDto categoryBrandDto) {
        //开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<CategoryBrand> brands =  brandCategoryMapper.selectBrandCategory(categoryBrandDto);
        PageInfo<CategoryBrand> pageInfo = new PageInfo<>(brands);
        System.err.println(pageInfo);
        return  pageInfo;
    }

    @Override
    public void addBrandCategory(CategoryBrandDto categoryBrandDto) {
        //添加前查询是否已经太添加过对应数据
        List<CategoryBrand> categoryBrands = brandCategoryMapper.selectBrandCategory(categoryBrandDto);
        boolean empty = CollUtil.isEmpty(categoryBrands);
        //查不到数据就可以进行添加
        if (empty) {
            brandCategoryMapper.insertBrandCategory(categoryBrandDto);
        }else {
            //查到数据说明数据已经存在抛异常
            throw  new ZhongQiaoException(ResultCodeEnum.BRAND_CATEGORY_EXIST);
        }

    }

    @Override
    public void modifyBrandCategory(CategoryBrandDto categoryBrandDto) {
        //修改前查询激励是否存在
        List<CategoryBrand> categoryBrands = brandCategoryMapper.selectBrandCategory(categoryBrandDto);
        boolean empty = CollUtil.isEmpty(categoryBrands);
        //查不到数据就可以进行添加
        if (empty) {
            brandCategoryMapper.updateBrandCategory(categoryBrandDto);
        }else {
            //查到数据说明数据已经存在抛异常
            throw  new ZhongQiaoException(ResultCodeEnum.BRAND_CATEGORY_EXIST);
        }
    }

    @Override
    public void removeBrandCategory(CategoryBrandDto categoryBrandDto) {
        brandCategoryMapper.deleteCategoryBrand(categoryBrandDto);
    }

    @Override
    public List<Brand> findBrandByCategoryId(int categoryId) {
        return brandCategoryMapper.selectBrandListByCategoryId(categoryId);
    }
}
